﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mime;
using System.Data.Linq;

namespace Baosight.Ccs.Mail
{
    public partial class Mail
    {
        private POP3_ClientMessage originalMessage;

        public POP3_ClientMessage OriginalMessage
        {
            get { return originalMessage; }
            set { originalMessage = value; }
        }

    }

    public static class MailHelper
    {
        /// <summary>
        /// 从<see cref="POP3_ClientMessage"/>获取Mail对象
        /// </summary>
        /// <param name="mesasge"></param>
        /// <returns></returns>
        public static Mail GetMailFromPop3Message(this POP3_ClientMessage message)
        {

            Mail mail = new Mail();
            mail.MailId = Guid.NewGuid();
            mail.MessageId = message.UID;

            Mime mailMime = Mime.Parse(message.MessageToByte());

            mail.Subject = mailMime.MainEntity.Subject;
            mail.FromAddress = mailMime.MainEntity.From.ToAddressListStringWithoutDisplayName();
            mail.ToAddress = mailMime.MainEntity.To.ToAddressListStringWithoutDisplayName();
            mail.CcAddress = mailMime.MainEntity.Cc.ToAddressListStringWithoutDisplayName();
            mail.BccAddress = mailMime.MainEntity.Bcc.ToAddressListStringWithoutDisplayName();
            mail.StrBody = mailMime.BodyText;
            mail.HtmlBody = String.IsNullOrEmpty(mailMime.BodyHtml)?String.Empty:mailMime.BodyHtml;
            mail.HtmlFlag = !String.IsNullOrEmpty(mailMime.BodyHtml);
            mail.SendUTC = mailMime.MainEntity.Date.ToUniversalTime();
            mail.ReceiveUTC = DateTime.Now.ToUniversalTime();
            mail.OriginalMessage = message;
            
            foreach (var item in mailMime.Attachments)
            {
                Attachment attachment = new Attachment();
                attachment.Id = Guid.NewGuid();
                attachment.FileName = item.ContentDisposition_FileName;
                attachment.FileContent = new Binary(item.Data);
                mail.Attachments.Add(attachment);
            }
            return mail;
        }


    }

    public static class AddressListExtension
    {
        /// <summary>
        /// 获取不带显示名称的邮件列表
        /// 因为显示名称会出现=?gb2312?B?=需要做encoding /decoding的操作，避免麻烦去处display name
        /// </summary>
        /// <param name="addressList"></param>
        /// <returns></returns>
        public static string ToAddressListStringWithoutDisplayName(this AddressList addressList)
        {
            if (addressList == null)
            {
                return String.Empty;
            }
            StringBuilder mailAddressWithComma = new StringBuilder();
            foreach (var address in addressList.Mailboxes)
            {
                mailAddressWithComma.Append(address.EmailAddress).Append(',');
            }
            if (mailAddressWithComma.Length > 0)
            {
                //remove last ,
                mailAddressWithComma.Remove(mailAddressWithComma.Length - 1, 1);
            }
            return mailAddressWithComma.ToString();
        }
    }
}
